package com.kehutong.contact.service.impl;

import com.kehutong.contact.entity.ContactComment;
import com.kehutong.contact.service.ContactCommentService;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.jdbc.JdbcSession;


import java.util.Objects;

/**
 * @author liujinwang
 * description:
 */
@WebService("/crm/contact/comment/service")
public class ContactCommentServiceImpl implements ContactCommentService {
    private static final Logger logger = LoggerFactory.getLogger(ContactCommentServiceImpl.class);

    @Inject
    private JdbcSession jdbcSession;

    @Override
    public Object page(JSONObject jsonObject) {
        String contactDynamicsId = jsonObject.getString("contactDynamicsId");
        if (Objects.isNull(contactDynamicsId)) {
            return Result.error(0,"contactDynamicsId不能为空");
        }
        return jdbcSession.findPage(ContactComment.class)
                .eq(jsonObject, "contactDynamicsId")
                .order("createTime", "asc")
                .page(jsonObject)
                .exe();
    }

    @Override
    public Object save(ContactComment comment) throws Exception {
        if (Objects.isNull(comment.getContactDynamicsId())) {
            return Result.error(0,"跟进id不能为空");
        }
        jdbcSession.insert(comment);
        return Result.success(comment);
    }

    @Override
    public Object delete(ContactComment comment) throws Exception {
        jdbcSession.deleteById(comment);
        return Result.success();
    }
}
